<template>
  <div class="member-order">
    {{ params }}
    <XtxTabs v-model="activeName" @tab-click="handleClick">
      <XtxTabsPanel
        v-for="item in orderStatus"
        :key="item"
        :label="item.label"
        :name="item.name"
      >
      </XtxTabsPanel>
    </XtxTabs>
    <!-- 商品列表 -->
    <div class="order-list">
      <div class="loading" v-if="loading"></div>
      <div class="none" v-if="!loading && !list.length">暂无数据</div>
      <OrderItem v-for="item in list" :key="item" :order="item"></OrderItem>
      <!-- 分页组件 -->
      <XtxPagination
        v-if="total > params.pageSize"
        :total="total"
        :current-page="params.page"
        :pageSize="params.pageSize"
        @current-change="params.page=$event"
      />
    </div>
  </div>
  <!-- <div style="transform: translateZ(0);"> -->
  <Teleport to="#dailog">
    <OrderCancel ref="orderCancelCom" />
  </Teleport>
  <!-- </div> -->
</template>

<script>
// 订单状态
import { reactive, ref, watch } from 'vue'
import { orderStatus } from '@/api/constants'
import { findOrderList } from '@/api/order'
import OrderItem from './components/order-item.vue'
import OrderCancel from './components/order-cancel.vue'
export default {
  components: { OrderItem, OrderCancel },
  name: 'MemberOrder',
  setup () {
    const activeName = ref('all')
    const handleClick = ({ index }) => {
      // console.log(val, '自定义事件获取到值')
      // 接口参数刚好和下标能对应起来，所以下标的值作为筛选参数
      params.orderState = index
      params.page = 1
    }

    // 请求参数 - 分页和筛选
    const params = reactive({
      page: 1,
      pageSize: 10,
      orderState: 0
    })

    const list = ref([])
    const total = ref(0)
    // 加载状态
    const loading = ref(false)
    watch(params, async () => {
      // 请求前显示加载
      loading.value = true
      const { result } = await findOrderList(params)
      list.value = result.items
      total.value = result.counts
      // 请求成功后隐藏加载
      loading.value = false
    }, { immediate: true })

    return { activeName, handleClick, orderStatus, list, params, loading, total }
  }
}
</script>

<style scoped lang="less">
.order-list {
  padding: 20px;
  position: relative;
  min-height: 600px;
  background-color: #fff;
}
.loading {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: rgba(255, 255, 255, 0.9) url(~@/assets/images/loading.gif)
    no-repeat center;
}
.none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}
</style>
